//
//  UIColor.swift
//  inke
//
//  Created by sh on 17/1/21.
//  Copyright © 2017年 畅联九州. All rights reserved.
//

import Foundation
import UIKit

extension UIColor {
    
    // 导航条颜色
    class var nav_color : UIColor {
        return UIColor(hexColor:"FFFFFF");
    }
    
    class var viewLineColor:UIColor {
        return UIColor.init(hexColor: "#eeeeee")
    }
    
    // 控制器的背景颜色
    class var backgroundColor:UIColor {
        return UIColor(hexColor: "#f7f7f7")
    }
    
    convenience init(red redValue:CGFloat, green greenValue:CGFloat, blue blueValue:CGFloat, _alpha alphaValue:CGFloat = 1.0) {
        self.init(red: redValue/255.0, green: greenValue/255.0, blue: blueValue/255.0, alpha: alphaValue)
    }
    
    convenience init(hexColor: String) {
        var red:   CGFloat = 0.0
        var green: CGFloat = 0.0
        var blue:  CGFloat = 0.0
        var alpha: CGFloat = 1.0
        var value = hexColor
        if hexColor.hasPrefix("#") {
            let index = hexColor.index(hexColor.startIndex, offsetBy: 1)
            value = String(hexColor.suffix(from:index))
        }
        let scanner = Scanner(string: value)
        var hexValue: CUnsignedLongLong = 0
        if scanner.scanHexInt64(&hexValue) {
            switch (value.count) {
            case 3:
                red   = CGFloat((hexValue & 0xF00) >> 8)       / 15.0
                green = CGFloat((hexValue & 0x0F0) >> 4)       / 15.0
                blue  = CGFloat(hexValue & 0x00F)              / 15.0
            case 4:
                red   = CGFloat((hexValue & 0xF000) >> 12)     / 15.0
                green = CGFloat((hexValue & 0x0F00) >> 8)      / 15.0
                blue  = CGFloat((hexValue & 0x00F0) >> 4)      / 15.0
                alpha = CGFloat(hexValue & 0x000F)             / 15.0
            case 6:
                red   = CGFloat((hexValue & 0xFF0000) >> 16)   / 255.0
                green = CGFloat((hexValue & 0x00FF00) >> 8)    / 255.0
                blue  = CGFloat(hexValue & 0x0000FF)           / 255.0
            case 8:
                red   = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
                green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
                blue  = CGFloat((hexValue & 0x0000FF00) >> 8)  / 255.0
                alpha = CGFloat(hexValue & 0x000000FF)         / 255.0
            default:
                break;
            }
        }
        self.init(red:red, green:green, blue:blue, alpha:alpha)
    }
    
    convenience init(hexColor: String, alpha:CGFloat) {
        var red:   CGFloat = 0.0
        var green: CGFloat = 0.0
        var blue:  CGFloat = 0.0
        var value = hexColor
        if hexColor.hasPrefix("#") {
            let index = hexColor.index(hexColor.startIndex, offsetBy: 1)
            value = String(hexColor.suffix(from: index))
        }
        let scanner = Scanner(string: value)
        var hexValue: CUnsignedLongLong = 0
        if scanner.scanHexInt64(&hexValue) {
            switch (value.count) {
            case 3:
                red   = CGFloat((hexValue & 0xF00) >> 8)       / 15.0
                green = CGFloat((hexValue & 0x0F0) >> 4)       / 15.0
                blue  = CGFloat(hexValue & 0x00F)              / 15.0
            case 6:
                red   = CGFloat((hexValue & 0xFF0000) >> 16)   / 255.0
                green = CGFloat((hexValue & 0x00FF00) >> 8)    / 255.0
                blue  = CGFloat(hexValue & 0x0000FF)           / 255.0
            default:
                break
            }
        }
        self.init(red:red, green:green, blue:blue, alpha:alpha)
    }
}
